package year2019.month08;

import java.util.Objects;

/**
 * 根据equals方法简单实现的<key,value>字典
 * @param <K>
 * @param <V>
 */
public class EqualsMap<K,V> {
    class Node<K, V>{
        final K key;
        V value;
        Node(K key, V value){
            this.key=key;
            this.value=value;
        }
        public final K getKey(){
            return key;
        }
        public final V getValue(){
            return value;
        }
        public V setValue(V newValue){
            V oldValue = value;
            value = newValue;
            return oldValue;
        }
        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }
    }

    private Node<K, V>[] elementTable;
    private int size;

    public EqualsMap(){
        elementTable= (Node<K, V>[]) new Node[10];
    }

    public EqualsMap(int size){
        if (size>0){
            elementTable = (Node<K, V>[]) new Node[size];
        } else if (size==0){
            elementTable = (Node<K, V>[]) new Node[0];
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+ size);
        }
    }


    public V put(K key, V value){
        int oldCapacity = elementTable.length;
        if (size>=oldCapacity-1){
            Node<K, V>[] newElementTable = (Node<K, V>[]) new Node[oldCapacity+(oldCapacity>>1)];  //每次增长一倍长度
            System.arraycopy(elementTable,0,newElementTable,0,oldCapacity);
            elementTable=newElementTable;
        }
        Node<K,V> newNode=new Node<>(key,value);
        Node<K,V> node=null;
        for (int i=0; i<size; i++){
            Node<K,V> element = elementTable[i];
            if (element.getKey().equals(key)){
                node = element;
                break;
            }
        }
        V oldValue = null;
        if (node==null){
            elementTable[size++]=newNode;
        } else {
            oldValue = node.getValue();
            node.setValue(value);
        }
        return oldValue;
    }

    public V get(K key){
        Node<K,V> node=null;
        for (int i=0; i<size; i++){
            Node<K,V> element = elementTable[i];
            if (element.getKey().equals(key)){
                node = element;
                break;
            }
        }
        return (V) node.getValue();
    }


    public static void main(String[] args){
        EqualsMap equalsMap = new EqualsMap();
        equalsMap.put("key","valueMy");
        equalsMap.put("key","valueMyChange");
        equalsMap.put("key","valueMyNow");
        equalsMap.put("key1","valueMy1");
        equalsMap.put("key2","valueMy2");
        equalsMap.put("key3","valueMy2");
        equalsMap.put("key4","valueMy2");
        equalsMap.put("key5","valueMy2");
        equalsMap.put("key6","valueMy2");
        equalsMap.put("key7","valueMy2");
        equalsMap.put("key8","valueMy2");
        equalsMap.put("key9","valueMy2");
        equalsMap.put("key10","valueMy2");
        equalsMap.put("key11","valueMy2");
        equalsMap.put("key12","valueMy2");
        System.out.println(equalsMap.get("key"));
        System.out.println(equalsMap.get("key1"));
        System.out.println(equalsMap.get("key2"));

    }

}
